<?php

namespace App\Admin\Controllers;

use App\Models\Record;
use App\Models\User;
use App\Models\Department;
use App\Models\Business;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;
use Encore\Admin\Facades\Admin;

class RecordController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '数据记录管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Record);

        $grid->column('id', 'Id');

        $grid->column('department_id','所属部门')->display(function () {
            return $this->department->name.'<点击查看责任人>';
        })->expand(function ($model) {
            $users = $model->users()->get();
            $users = $users->map(function ($user) {
                return $user->only(['id', 'name']);
            });
            return new Table(['ID', '责任人'], $users->toArray());
        });

        $grid->column('business_id','业务')->display(function () {
            return $this->business->name;
        });

        $grid->column('value', '完成');
        $grid->column('score', '得分');


        $grid->column('evaluation_result','评价结果')->display(function () {
            return $this->evaluation_result;
        })->using([
            '优秀' => '优秀',
            '良好' => '良好',
            '一般' => '一般',
            '关注' => '关注',
            '重点关注' => '重点关注',
        ],'未评价')->label([
            '优秀' => 'success',
            '良好' => 'primary',
            '一般' => 'default',
            '关注' => 'warning',
            '重点关注' => 'danger',
        ],'error');

        $grid->column('record_date','数据日期')->date('Y-m-d')->sortable();

        $grid->filter(function ($filter) {
            $filter->column(1/2, function ($filter) {
                $filter->disableIdFilter();
                $filter->equal('business_id', '业务类型')->select('businesses/getBusiness');
                $filter->equal('department_id','所属部门')->select('departments/getDepartment');
            });
            $filter->column(1/2, function ($filter){
                $filter->between('record_date', '开始时间')->date();
                $filter->equal('evaluation_result', '评价结果')->select([
                    '优秀'=>'优秀',
                    '良好'=>'良好',
                    '一般'=>'一般',
                    '关注'=>'关注',
                    '重点关注'=>'重点关注',
                ]);
            });

        });

        return $grid;
    }


    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Record::findOrFail($id));



        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Record);

        $form->display('id', 'ID');
        $form->display('record_date', '数据时间');
        $form->select('department_id','所属部门')->options(Department::selectOptions());
        $form->select('business_id','业务类型')->options(Business::selectOptions());

        $form->number('value', '实际完成')->default(0);
        $form->number('score', '得分')->default(0.0);

        $form->select('evaluation_result','评价结果')->options([
            '优秀'=>'优秀',
            '良好'=>'良好',
            '一般'=>'一般',
            '关注'=>'关注',
            '重点关注'=>'重点关注',
        ]);

        $form->textarea('comment','业务点评');
        $form->listbox('users','负责人')->options(User::all()->pluck('name', 'id'));

        $form->display('created_at', '记录创建时间');
        $form->display('updated_at', '记录更新时间');

        return $form;
    }

    public function store()
    {
        return parent::store();
    }

    public function update($id)
    {
        $record = Record::find($id);
        if ($record->evaluation_result != request()->evaluation_result) {
            $record->is_evaluated_by_hand = true;
            $record->analyzer_id = Admin::user()->id;
            $record->save(); //更新为手工评价
        }
        return parent::update($id);
    }


}
